package com.lxl.bt.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxl.bt.R;
import com.lxl.bt.activity.BaseFragment;
import com.lxl.bt.activity.ProductDetailActivity;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.EnvironmentUtil;
import com.lxl.bt.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Create by lvxinlong
 * on btc
 * on 2021年3月6日10:00:20
 **/
public class YouHuiQuanListFragment extends BaseFragment {


    @BindView(R.id.rcv)
    RecyclerView rcv;
    @BindView(R.id.llNodata)
    LinearLayout llNodata;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private Unbinder unbinder;
    ACache aCache;
    BaseQuickAdapter<String, BaseViewHolder> sysAdapter;
    int pageNo = 1;
    int parse = 0;

    public static YouHuiQuanListFragment newInstance(String type) {
        Bundle bundle = new Bundle();
        YouHuiQuanListFragment fragment = new YouHuiQuanListFragment();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_btclist, container, false);
//        ((BaseActivity) getActivity()).setStatusBarFullTransparent();

        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, view);
//        EventBus.getDefault().register(this);
        aCache = ACache.get(getActivity());
        sysAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_youhuiquan) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, String bean) {
            }


            @Override
            public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int position) {

            }
        };
        rcv.setAdapter(sysAdapter);
//        rcv.addItemDecoration(new MyDividerDecoration(Color.TRANSPARENT, 16));
        sysAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), ProductDetailActivity.class));
            }
        });
        iniSmartRefreshLayout();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    private void iniSmartRefreshLayout() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (!EnvironmentUtil.isNetConnect(getActivity())) {
                    ToastUtil.showToast(getActivity(), "暂无网络");
                    refreshLayout.finishRefresh();
                    return;
                }
                parse=0;
                updateData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                parse++;
                getData();
            }
        });

    }

    private void updateData() {
        pageNo = 1;
        getData();
    }

    @Override
    public void getData() {
        notifyList();
    }

    public void notifyList() {
        List<String>ss=new ArrayList<>();
        sysAdapter.setNewData(ss);
//        JsonObject jsonObject = new JsonObject();
//        jsonObject.addProperty("pageNo", pageNo);
//        jsonObject.addProperty("pageSize", 20);
//        RetrofitUtil.getInstance().create(NetApi.class, 0)
//                .notifyList(Prefs.getStringValue(Constant.JSESSIONID), jsonObject).enqueue(new NetCallback<Result<NotifyBody>>() {
//            @Override
//            public void onSuccess(Result<NotifyBody> result) {
//                if (result.isSuccess()) {
//                    refreshLayout.finishRefresh();
//                    refreshLayout.finishLoadMore();
//                    if (pageNo == 1)
//                        sysAdapter.setNewData(result.getData().getOaNotifyList());
//                    else
//                        sysAdapter.addData(result.getData().getOaNotifyList());
//
//                    sysAdapter.setEnableLoadMore(result.getData().totalPage > pageNo ? true : false);
//                    pageNo++;
//                }
//            }
//
//            @Override
//            public void netLogin() {
//
//            }
//
//            @Override
//            public void onError(String errorStr) {
//                super.onError(errorStr);
//                Log.w("lxl", errorStr);
//
//            }
//
//        });
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }


//    @Subscribe
//    public void onEventMainThread(EventTagBean event) {
//    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    }

}
